<!DOCTYPE html>
<html>
<head>
    <title locale="title/title">File Properties</title>
	<meta name="mobile-web-app-capable" content="yes">
	<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1, maximum-scale=1"/>
	<meta charset="UTF-8">
    <link rel="stylesheet" href="../../script/semantic/semantic.min.css">
    <script src="../../script/jquery.min.js"></script>
	<script src="../../script/semantic/semantic.min.js"></script>
    <script type="text/javascript" src="../../script/ao_module.js"></script>
    <script type="text/javascript" src="../../script/applocale.js"></script>
    <link rel="stylesheet" href="../../script/ao.css">
    <style>
        .hidden{
            display:none;
        }
    
        .disabled{
            opacity: 0.5;
            pointer-events: none;
        }

        .colorblock{
            width: 20px;
            height: 20px;
            display: inline-block;
            vertical-align: bottom;
            margin-right: 12px;
        }

        .blue.colorblock{
            background-color: #52bdf2;
        }

        .grey.colorblock{
            background-color: #b9b9b9;
        }

        .yellow.colorblock{
            background-color: #e5e75c;
        }

        .ui.table tr td{
           border-top: 0px solid transparent !important; 
        }
        
        .overlap.bar{
            position: absolute !important; 
            top:0px; 
            left: 0px;
        }

        .smallerFont{
            font-size: 98%;
        }

        /* Theme overwrite */
        body{
            background-color: var(--body_background);
            color: var(--text_color);
        }

        input{
            background-color: var(--body_background_secondary) !important;
            color: var(--text_color_secondary) !important;
            border: 1px solid var(--body_background_active) !important;
        }

        button{
            background-color: var(--body_background_active) !important;
            color: var(--text_color) !important;
        }
        button:hover{
            opacity: 0.8;
        }

        td,.header,p,div,span{
            color: var(--text_color) !important;
        }
    </style>
</head>
<body class="whiteTheme">
    <br>
    <div class="ui container">
        <h3 class="ui header">
            <span locale="title/title">Virtual Disk Properties</span>
            <div class="sub header" locale="title/desc">Show information about vroot mount points</div>
        </h3>
        <div class="ui divider"></div>
        <div class="ui container">
            <div class="ui grid">
                <div class="three wide right aligned column" style="text-align: center; padding: 0px;">
                    <img class="ui image" style="position: absolute; top: 0px; left: calc(50% - 25px); max-width: 50px; pointer-events: none;" src="../../img/system/drive-virtual.svg">
                </div>
                <div class="thirteen wide column" style="padding: 0px; padding-top: 0.8em; padding-left: 1em;">
                    <div class="ui fluid small input">
                        <input id="vrootName" type="text" placeholder="" readonly="true">
                    </div>
                </div>
            </div>
        </div>
        <br>
        <div class="ui divider" style="margin-top: 8px; margin-bottom: 8px;"></div>
        <table class="ui very basic collapsing compact unstackable table" style="vertical-align: middle;">
            <tbody>
            <tr>
                <td locale="diskinfo/physical">
                    Physical Disk: 
                </td>
                <td id="phydisk">
                    
                </td>
            </tr>
            <tr>
                <td locale="diskinfo/hierarchy">
                    Hierarchy (Format): 
                </td>
                <td id="hierarchy">
                    
                </td>
            </tr>
        </table>
        <div class="ui divider" style="margin-top: 8px; margin-bottom: 8px;"></div>
        <table class="ui very basic collapsing compact unstackable table" style="vertical-align: middle; font-size:90%;">
            <tbody>
            <tr>
                <td>
                    <div class="blue colorblock"></div><span locale="spaceinfo/totalUsed">Total Used Space:</span>
                </td>
                <td>
                    <span id="usedSpaceInBytes"  class="smallerFont"></span> <span locale="unit/bytes">Bytes</span>
                </td>
                <td>
                    <span id="usedSpaceInHumanReadableFormat" class="smallerFont"></span>
                </td>
            </tr>
            <tr>
                <td>
                    <div class="yellow colorblock"></div><span locale="spaceinfo/userUsed">User Used Space:</span>
                </td>
                <td>
                    <span id="usedVirtualSpaceInByte" class="smallerFont"><i class="ui spinner loading icon"></i></span> <span locale="unit/bytes">Bytes</span>
                </td>
                <td>
                    <span id="usedVirtualSpaceInHumanReadableFormat"  class="smallerFont"></span>
                </td>
            </tr>
            <tr>
                <td>
                    <div class="grey colorblock"></div><span locale="spaceinfo/Usable">Usable Space:</span>
                </td>
                <td>
                    <span id="usableSpaceInBytes" class="smallerFont"></span> <span locale="unit/bytes">Bytes</span>
                </td>
                <td>
                    <span id="usableSpaceInHumanReadableFormat" class="smallerFont"></span>
                </td>
            </tr>
            <tr>
                <td>
                    <div class="transparent colorblock"></div><span locale="spaceinfo/Capacity">Capacity:</span>
                </td>
                <td>
                    <span id="totalSpaceInBytes" class="smallerFont"></span> <span locale="unit/bytes">Bytes</span>
                </td>
                <td>
                    <span id="totalSpaceInHumanReadableFormat" class="smallerFont"></span>
                </td>
            </tr>
        </table>
        <div class="ui small progress">
            <div class="overlap bar" style="width: 100%; background-color: #b9b9b9;">
                <div class="progress"></div>
            </div>
            <div id="totalUsedSpace" class="overlap bar" style="min-width: 5px; width: 100%; background-color: #52bdf2;">
                <div class="progress"></div>
            </div>
            <div id="userUsedSpace" class="overlap bar" style="min-width: 5px; width: 100%; background-color: #e5e75c;">
                <div class="progress"></div>
            </div>
        </div>
        <div class="ui divider" style="margin-top: 8px; margin-bottom: 8px;"></div>
        <br>
           <button class="ui right floated button" onclick="ao_module_close();" locale="button/close">Close</button>
        <br>
    </div>
        
    <br><br>
    <script>
        //Initiate the view model
        var files = ao_module_loadInputFiles();
        var fileProperties = [];
        var fileInfo = {};
        var diskInfo = {};

        try{
            applocale.init("../locale/disk_properties.json", function(){
                applocale.translate();
                init();
            });
        }catch(ex){
            init();
        }
        
        //Initialize system theme
        loadPreference("file_explorer/theme",function(data){
            if (data.error === undefined){
                if (data == "darkTheme"){
                    $("body").addClass("darkTheme");
                }else{
                    $("body").addClass("whiteTheme");
                }
            }
        });

       function init(){
            //There are something to load. Load the vroot properties
            if (window.location.hash.length > 0 && typeof(files) != null){
                var rootName = files[0].split("/")[0];
                var NALocaleString = applocale.getString("error/na", "[Not Available]");
                $("#vrootName").val(rootName + "/");

                getDiskProp(files[0], function(data){
                    if (data.error !== undefined){
                        $("#phydisk").text("Unknown");
                        $("#hierarchy").text("Unknown");
                        $("#usedSpaceInBytes").text(NALocaleString);
                        $("#usableSpaceInBytes").text(NALocaleString);
                        $("#totalSpaceInBytes").text(NALocaleString);
                        console.log(data.error);
                    }else{
                        if (data.Used == 0){
                            //Network drive
                            $("#usedSpaceInBytes").text(NALocaleString);
                            $("#usedSpaceInHumanReadableFormat").text("");
                        }else{
                            $("#usedSpaceInBytes").text(new Intl.NumberFormat('en-US').format(data.Used));
                            $("#usedSpaceInHumanReadableFormat").text(bytesToSize(data.Used));
                        }
                        

                        $("#usableSpaceInBytes").text(new Intl.NumberFormat('en-US').format(data.Available));
                        $("#usableSpaceInHumanReadableFormat").text(bytesToSize(data.Available));

                        $("#totalSpaceInBytes").text(new Intl.NumberFormat('en-US').format(data.Total));
                        $("#totalSpaceInHumanReadableFormat").text(bytesToSize(data.Total));

                        $("#phydisk").text(data.PhysicalDevice + "/");
                        var s = data.MountingHierarchy;
                        s = s[0].toUpperCase() + s.slice(1);
                        $("#hierarchy").text(applocale.getString("hierarchy/" + s, s) + ` (${data.FileSystemType.toUpperCase()})`);
                        
                        $("#totalUsedSpace").css("width", (data.Used / data.Total) * 100 + "%");
                    }

                    getFileProp(files[0], function(folderdata){
                        //User Used Space
                        if (folderdata.Filesize >= 0){
                            $("#usedVirtualSpaceInByte").text(new Intl.NumberFormat('en-US').format(folderdata.Filesize));
                            $("#usedVirtualSpaceInHumanReadableFormat").text(bytesToSize(folderdata.Filesize));
                        
                        }else{
                            $("#usedVirtualSpaceInByte").text(NALocaleString);
                            $("#usedVirtualSpaceInHumanReadableFormat").text("");
                        }

                        if (diskInfo.Used == 0 && folderdata.Filesize >= 0){
                            //Network or special drive. Use user space as usedSpace
                            $("#usedSpaceInBytes").text(new Intl.NumberFormat('en-US').format(folderdata.Filesize));
                            $("#usedSpaceInHumanReadableFormat").text(bytesToSize(folderdata.Filesize));
                        }else if (diskInfo.Used == 0){
                            $("#usedSpaceInBytes").text(NALocaleString);
                            $("#usedSpaceInHumanReadableFormat").text("");
                        }
                       
                        
                        if (data.Total != undefined && data.Total > 0){
                            $("#userUsedSpace").css("width", (folderdata.Filesize / data.Total) * 100 + "%");
                        }else{
                            //Error when reading disk size. Just make it full width
                            $("#userUsedSpace").css("width", "100%");
                        }
                        
                    });
                });
            }
       }

        function bytesToSize(bytes) {
            var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
            if (bytes == 0) return '0 Byte';
            var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
            return (bytes / Math.pow(1024, i) * 100 / 100).toFixed(1) + ' ' + sizes[i];
        }

        function getDiskProp(vpath, callback){
            $.ajax({
                url: "../../system/disk/space/resolve",
                data: {path: vpath},
                method: "POST",
                success: function(data){
                    diskInfo = data;
                    callback(data);
                }
            })
        }

        function getFileProp(vpath, callback){
            $.ajax({
                url: "../../system/file_system/getProperties",
                data: {path: vpath},
                method: "POST",
                success: function(data){
                    callback(data);
                    fileInfo = data;
                }
            })  
        }

        function loadPreference(key, callback){
            $.get("../../system/file_system/preference?key=" + key,function(data){
                callback(data);
            });
        }

    </script>
</body>
</html>